﻿
using CameraClient.CForm.CameraF;
using ChargingSystem4.CsForm;
using Edge.Repository.Model.CameraSys;
using Edge.Repository.Model.CameraSys.CDal;
using EMSClient.Config;
using Oliving.FormCommon.Oliving.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using WindowsFormsApp4.EdgeVlc;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace EMSClient.CForm
{
    public partial class FormMain : Form
    {

        #region 窗体移动
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {

            ReleaseCapture();
            SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);

        }


        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        [DllImport("user32.dll")]
        private static extern int ReleaseCapture();







        private void FormMain_ClientSizeChanged(object sender, EventArgs e)
        {
            PingXianConfig xianConfig = PingXianHelper.getPingXianConfig();
            foreach (Control control in flowLayoutPanel1.Controls)
            {
                if (control is EdgeVlcUc)
                {
                    EdgeVlcUc myItem = (EdgeVlcUc)control;
                    myItem.Width = (flowLayoutPanel1.Width / xianConfig.col) - 6;
                    myItem.Height = (flowLayoutPanel1.Height / xianConfig.row) - 6;
                }
            }

        }


        #endregion



        #region 基础 窗体   
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //dateTimePicker1.Value = DateTimeUtil.GetNowDayBeginTime();
            this.Text = EmsConfigHelper.SoftName;
            label1.Text = EmsConfigHelper.SoftName;

            InitCamera();

            //     ThreadHuiDu();


        }



        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("您确认退出吗？", "", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                Dispose();
                Process.GetCurrentProcess().Kill();
            }
            else
            {
                e.Cancel = true;
            }
        }



        #endregion



        #region 子窗体


        private void MyClass_CustomEvent(object sender, EventArgs e)
        {
            // 处理事件
            InitCamera();
        }
        public void InitCamera()
        {
            Splasher.Show(typeof(FrmSplashBox));
            Splasher.Status = "正在加载数据，请稍候... ...";

            foreach (Control control in flowLayoutPanel1.Controls)
            {
                if (control is EdgeVlcUc)
                {
                    EdgeVlcUc myItem = (EdgeVlcUc)control;
                    myItem.stop();
                }
            }

            flowLayoutPanel1.Controls.Clear();
            CameraService dal = new CameraService();

            PingXianConfig xianConfig = PingXianHelper.getPingXianConfig();
            List<CameraConfigModel> cameras = dal.Query().Take(xianConfig.row * xianConfig.col).ToList();


            foreach (CameraConfigModel item in cameras)
            {
                EdgeVlcUc uc = new EdgeVlcUc(item);

                // 订阅事件
                uc.CustomEvent += new CustomEventHandler(MyClass_CustomEvent);
                uc.Width = (flowLayoutPanel1.Width / xianConfig.col) - 6;
                uc.Height = (flowLayoutPanel1.Height / xianConfig.row) - 6;

                flowLayoutPanel1.Controls.Add(uc);
            }

            Splasher.Close();
        }


        #endregion


        #region 功能按钮

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormCameraEdit form = new FormCameraEdit(0);

            if (form.ShowDialog() == DialogResult.OK)
            {
                InitCamera();
            }
        }

        private void 全部打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Thread thread = new Thread(() =>
              {
                  foreach (Control control in flowLayoutPanel1.Controls)
                  {
                      if (control is EdgeVlcUc)
                      {
                          EdgeVlcUc myItem = (EdgeVlcUc)control;
                          myItem.Invoke(new EventHandler(delegate
                          {
                              myItem.play();
                          }));
                      }
                      Thread.Sleep(20);
                  }
              });
            thread.IsBackground = true;
            thread.Start();

        }



        private void 视频全关ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(() =>
            {
                foreach (Control control in flowLayoutPanel1.Controls)
                {
                    if (control is EdgeVlcUc)
                    {
                        EdgeVlcUc myItem = (EdgeVlcUc)control;
                        myItem.Invoke(new EventHandler(delegate
                        {
                            myItem.stop();
                        }));
                    }
                    Thread.Sleep(20);
                }
            });
            thread.IsBackground = true;
            thread.Start();

        }




        private void 屏显设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            FormPxsz form = new FormPxsz();

            if (form.ShowDialog() == DialogResult.OK)
            {
                InitCamera();
            }

        }

        private void 温度设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            FormWenDu form = new FormWenDu();

            if (form.ShowDialog() == DialogResult.OK)
            {
                InitCamera();
            }
        }

        private void 温度曲线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormChartAll form = new FormChartAll();
            form.ShowDialog();
        }


        #endregion




        private void panel5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认退出吗？", "", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                Dispose();
                Process.GetCurrentProcess().Kill();
            }
        }

        private void panel6_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void panel7_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

    
    }

}
